package kt.com.bilin.coin.ui.activity

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.R
import kotlinx.android.synthetic.main.activity_feedback.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.common.bindViewClick
import kt.com.bilin.coin.common.toastMessageLong
import kt.com.bilin.coin.listener.TextWatcher
import kt.com.bilin.coin.ui.adapter.UserImageAdapter
import kt.com.bilin.coin.utils.FullyGridLayoutManager
import kt.com.bilin.coin.utils.VariableViewUtils

@Route(path = "/v2/user/feedback")
class FeedbackActivity : BaseActivity(R.layout.activity_feedback), View.OnClickListener {
    private val mUserImageAdapter: UserImageAdapter by lazy { UserImageAdapter(activity = this, path = "feedback", changeCallback = this::changeCallback).apply { maxLength = 3 } }

    override fun initView() {
        bindViewClick(this, buttonSubmit)
        VariableViewUtils().apply {
            addOnValidationResultListener(buttonSubmit)
            addTextView(etContent)
            validator()
        }
        titleView.onRightTextClickListener = { goActicity(MyMessageActivity::class.java) }
        tvMax.text = getString(R.string.text_mine_feedback_max, 0)
        textImgsMax.text = getString(R.string.text_mine_feedback_imgs_max, 0)
        etContent.addTextChangedListener(TextWatcher { tvMax.text = getString(R.string.text_mine_feedback_max, it.length) })
        recyclerView.adapter = mUserImageAdapter
        recyclerView.layoutManager = FullyGridLayoutManager(this, 3)
    }

    private fun changeCallback() {
        textImgsMax.text = getString(R.string.text_mine_feedback_imgs_max, mUserImageAdapter.getOriginalSize())
    }

    override fun onClick(v: View?) {
        launchUI({ API_SERVICE.feedbackSubmit(etContent.text.toString(), mUserImageAdapter.getGalleryAlbums().joinToString(",")) }, ApiObserver(this, commonButton = buttonSubmit) {
            "反馈成功".toastMessageLong()
            finish()
        })
    }
}